package com.liuyi.netty.im.lychat.modular.caches;

import cn.hutool.core.convert.Convert;
import com.alicp.jetcache.anno.CacheInvalidate;
import com.alicp.jetcache.anno.CacheUpdate;
import com.alicp.jetcache.anno.Cached;
import com.liuyi.netty.im.commons.codes.data.event.Message;
import com.liuyi.netty.im.commons.codes.data.event.message.playload.Playload;
import com.liuyi.netty.im.commons.codes.data.event.message.playload.PlayloadText;
import com.liuyi.netty.im.lychat.modular.entity.ChatSession;
import com.liuyi.netty.im.lychat.modular.service.IChatSessionService;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.util.Pool;

import javax.annotation.Resource;

/**
 * 聊天会话缓存
 *
 * @author Mr.Fmy
 * @since 2021/12/30
 */
@Service
public class ChatSessionCache {

    @Resource
    private IChatSessionService chatSessionService;
    @Resource
    private Pool<Jedis> jedisPool;

    private final String KEY = "ChatSession:";

    @Cached(name = KEY, key = "'getByUserIdAndSessionId:'+#userId+':sessionId:'+#sessionId")
    public ChatSession getByUserIdAndSessionId(String userId, String sessionId) {
        return chatSessionService.getOne(chatSessionService.q().eq(ChatSession.USER_ID, userId)
                .eq(ChatSession.SESSION_ID, sessionId));
    }

    @CacheInvalidate(name = KEY, key = "'getByUserIdAndSessionId:'+#userId+':sessionId:'+#sessionId")
    public void removeUserIdAndSessionId(String userId, String sessionId) {
    }

    @Cached(name = KEY, key = "'getLastMessage:'+#userId+':sessionId:'+#sessionId")
    public Message<? extends Playload> getLastMessage(String userId, String sessionId) {
        Message<PlayloadText> lastMessage = new Message<>();
        lastMessage.setCreateTime(System.currentTimeMillis());
        PlayloadText text = PlayloadText.PlayloadTextBuilder.aPlayloadText().text("").build();
        lastMessage.setPlayload(text);
        return lastMessage;
    }

    @CacheUpdate(name = KEY, key = "'getLastMessage:'+#userId+':sessionId:'+#sessionId", value = "#message")
    public void updadteLastMessage(String userId, String sessionId, Message<? extends Playload> message) {
    }

    @CacheInvalidate(name = KEY, key = "'getLastMessage:'+#userId+':sessionId:'+#sessionId")
    public void removeLastMessage(String userId, String sessionId) {
    }

    /**
     * 获取未读计数
     *
     * @param userId    用户id
     * @param sessionId 会话id
     * @return {@link Long}
     */
    public Long getUnreadCount(String userId, String sessionId) {
        try (Jedis jedis = jedisPool.getResource()) {
            String obj = jedis.get(KEY.concat("UnreadCount:").concat(userId).concat(":sessionId:").concat(sessionId));
            return Convert.toLong(obj, 0L);
        }
    }

    /**
     * 增加未读计数
     *
     * @param userId    用户id
     * @param sessionId 会话id
     * @return {@link Long}
     */
    public Long incrUnreadCount(String userId, String sessionId) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.incr(KEY.concat("UnreadCount:").concat(userId).concat(":sessionId:").concat(sessionId));
        }
    }

    /**
     * 重置未读计数
     *
     * @param userId    用户id
     * @param sessionId 会话id
     */
    public void resetUnreadCount(String userId, String sessionId) {
        try (Jedis jedis = jedisPool.getResource()) {
            jedis.del(KEY.concat("UnreadCount:").concat(userId).concat(":sessionId:").concat(sessionId));
        }
    }
}
